package com.android.jianboshi.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.tool.SPUtil;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.MultiStateView;
import com.android.jianboshi.R;
import com.android.jianboshi.bean.DataInfo;
import com.android.jianboshi.constants.SPConstants;
import com.android.jianboshi.dialog.Boxing_PowerResult_DialogFragment;
import com.android.jianboshi.dialog.DaoJiShi_DialogFragment;
import com.android.jianboshi.event.Event_Notification;
import com.android.jianboshi.manager.BoxingHelper;
import com.android.jianboshi.manager.BoxingManager;
import com.android.jianboshi.manager.MedioPlayerUtil;
import com.android.jianboshi.ui.view.Tips_View;
import com.yy.mobile.rollingtextview.CharOrder;
import com.yy.mobile.rollingtextview.RollingTextView;
import com.yy.mobile.rollingtextview.strategy.Strategy;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

public class Boxing_PowerActivity extends BaseActivity {

    @BindView(R.id.multiplestatusView)
    MultiStateView multiplestatusView;
    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.roll_text1)
    RollingTextView roll_text1;
    @BindView(R.id.roll_text2)
    RollingTextView roll_text2;
    @BindView(R.id.roll_text3)
    RollingTextView roll_text3;
    @BindView(R.id.tv_retry)
    TextView tv_retry;
    @BindView(R.id.tips_view)
    Tips_View tips_view;
    @BindView(R.id.tv_unit)
    TextView tv_unit;
    @BindView(R.id.tv_result_over)
    TextView tv_result_over;
    @BindView(R.id.rl_result)
    RelativeLayout rl_result;



    Handler handler = new Handler();
    MedioPlayerUtil playerUtil;

    BoxingManager boxingManager = new BoxingManager();
    boolean isBegin = false;  //开始计数

    @Override
    protected int getLayoutId() {
        return R.layout.activity_boxing_power;
    }

    @Override
    protected void initView() {
        comm_title.setTitle(getString(R.string.boxing_power));
        roll_text1.setAnimationDuration(300L);
        roll_text1.setCharStrategy(Strategy.NormalAnimation());
        roll_text1.addCharOrder(CharOrder.Number);
        roll_text2.setAnimationDuration(300L);
        roll_text2.setCharStrategy(Strategy.NormalAnimation());
        roll_text2.addCharOrder(CharOrder.Number);
        roll_text3.setAnimationDuration(300L);
        roll_text3.setCharStrategy(Strategy.NormalAnimation());
        roll_text3.addCharOrder(CharOrder.Number);

        DaoJiShi_DialogFragment.showDialog(this, null).setCallback(new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                //倒计时结束
                Log.i("倒计时结束");

                playerUtil = new MedioPlayerUtil(mContext, null);
                playerUtil.setData("music/dida.mp3", false);

                isBegin = true;

                handler.postDelayed(runnable, 0);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tv_retry.setVisibility(View.VISIBLE);
                        handler.removeCallbacks(runnable);
                        playerUtil.stop();

                        isBegin = false;
                        //5秒钟结束，计算结果
                        jisuanResult();
                    }
                }, 5000);
            }
        });

        //设置单位
        int unit = SPUtil.getIntValue(mContext, SPConstants.Unit, 0);
        if (unit == 0) {
            tv_unit.setText(getString(R.string.power_unit_unit1));
        } else {
            tv_unit.setText(getString(R.string.power_unit_unit2));
        }
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Random r = new Random();
            int temp1 = r.nextInt(10);
            int temp2 = r.nextInt(10);
            int temp3 = r.nextInt(10);
            roll_text1.setText(temp1 + "");
            roll_text2.setText(temp2 + "");
            roll_text3.setText(temp3 + "");

            handler.postDelayed(runnable, 200);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
        playerUtil.stop();
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
    }

    @OnClick({R.id.tv_show_record, R.id.tv_retry})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_show_record:
                Map<String, Object> params = new HashMap<>();
                double Boxing_Power_Max_Power = Double.parseDouble(SPUtil.getStringValue(mContext, SPConstants.Boxing_Power_Max_Power, "0"));
                params.put(Boxing_PowerResult_DialogFragment.Param_Power, Boxing_Power_Max_Power);
                Boxing_PowerResult_DialogFragment.showDialog(this, params);
                break;
            case R.id.tv_retry:
                startActivity(new Intent(mContext, Boxing_PowerActivity.class));
                finish();
                break;
        }
    }


    @Override
    protected boolean isRegistEventbus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event_Notification event) {
        if (isBegin) {
            Log.i("数据");
            boxingManager.parseScanResult(event.data);
        }
        tips_view.setData(event.data);
    }

    //查看当前成绩
    private void jisuanResult() {
        double maxPower = 0;
        for (DataInfo air : boxingManager.airPressureList) {
            double power = BoxingHelper.getPower(air);
            if (power > maxPower) {
                maxPower = power;
            }
        }
        Log.i("maxPower:" + maxPower);
        //力量大于400， 显示过载
        if(maxPower>400)
        {
            tv_result_over.setVisibility(View.VISIBLE);
            rl_result.setVisibility(View.GONE);
            return;
        }
        double Boxing_Power_Max_Power = Double.parseDouble(SPUtil.getStringValue(mContext, SPConstants.Boxing_Power_Max_Power, "0"));
        if (maxPower > Boxing_Power_Max_Power) {
            SPUtil.putValue(mContext, SPConstants.Boxing_Power_Max_Power, maxPower + "");
        }


        //转换单位
        maxPower = BoxingHelper.getPowerByUnit(mContext,maxPower);

        //个位数：
        int num1 = (int) (maxPower % 10);
        //十位数：
        int num2 = (int) (maxPower / 10 % 10);
        //百位数
        int num3 = (int) (maxPower / 100 % 10);

        Log.i("num1:" + num1 + " num2:" + num2 + " num3:" + num3);

        roll_text1.setText(num3 + "");
        roll_text2.setText(num2 + "");
        roll_text3.setText(num1 + "");

    }

}
